package com.wy.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializeUtils {
    //序列化
    public static byte[] serialize(Object object){
        ObjectOutputStream outputStream = null;
        ByteArrayOutputStream bOutputStream = null;

        try{
            bOutputStream = new ByteArrayOutputStream();
            outputStream = new ObjectOutputStream(bOutputStream);
            outputStream.writeObject(object);
            byte[] bytes = bOutputStream.toByteArray();
            return  bytes;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;

    }

    //反序列化
    public static Object unserialize(byte[] bytes){
        ByteArrayInputStream byteArrayInputStream = null;
        ObjectInputStream objectInputStream = null;
        try{
            byteArrayInputStream = new ByteArrayInputStream(bytes);
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            return objectInputStream.readObject();
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
